package com.plugin.manage.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plugin.core.model.entity.ElderCheckOut;
import com.plugin.manage.common.PageResult;
import com.plugin.manage.mapper.CheckoutMapper;
import com.plugin.manage.service.ICheckoutService;
import com.plugin.manage.pojo.vo.ElderCheckoutApplymentVO;
import com.plugin.manage.pojo.vo.ElderCheckoutDynamicVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 退住记录服务实现类
 *
 * @author mark
 * @date 2025-08-04
 */
@Service
@Slf4j
public class CheckoutServiceImpl extends ServiceImpl<CheckoutMapper, ElderCheckOut> implements ICheckoutService {

    @Autowired
    private CheckoutMapper checkoutMapper;

    /**
     * 获取总退住记录数量（不筛选approval_status）
     * 
     * @return 总退住记录数量
     */
    @Override
    public Long getTotalCheckoutCount() {
        log.info("开始获取总退住记录数量");
        Long count = checkoutMapper.countTotalCheckouts();
        log.info("总退住记录数量：{}", count);
        return count;
    }
    
    /**
     * 获取approval_status=1的退住记录数量
     * 
     * @return approval_status=1的退住记录数量
     */
    @Override
    public Long getStatus1CheckoutCount() {
        log.info("开始获取approval_status=1的退住记录数量");
        Long count = checkoutMapper.countStatus1Checkouts();
        log.info("approval_status=1的退住记录数量：{}", count);
        return count;
    }
    
    /**
     * 获取approval_status=2的退住记录数量
     * 
     * @return approval_status=2的退住记录数量
     */
    @Override
    public Long getStatus2CheckoutCount() {
        log.info("开始获取approval_status=2的退住记录数量");
        Long count = checkoutMapper.countStatus2Checkouts();
        log.info("approval_status=2的退住记录数量：{}", count);
        return count;
    }
    
    /**
     * 获取approval_status=3的退住记录数量
     * 
     * @return approval_status=3的退住记录数量
     */
    @Override
    public Long getStatus3CheckoutCount() {
        log.info("开始获取approval_status=3的退住记录数量");
        Long count = checkoutMapper.countStatus3Checkouts();
        log.info("approval_status=3的退住记录数量：{}", count);
        return count;
    }
    
    /**
     * 获取approval_status=4的退住记录数量
     * 
     * @return approval_status=4的退住记录数量
     */
    @Override
    public Long getStatus4CheckoutCount() {
        log.info("开始获取approval_status=4的退住记录数量");
        Long count = checkoutMapper.countStatus4Checkouts();
        log.info("approval_status=4的退住记录数量：{}", count);
        return count;
    }
    
    /**
     * 获取所有审批状态的退住记录数量
     * 
     * @return 包含所有审批状态数量的Map
     */
    @Override
    public Map<String, Long> getAllStatusCheckoutCounts() {
        log.info("开始获取所有审批状态的退住记录数量");
        Map<String, Long> statusCounts = checkoutMapper.countAllStatusCheckouts();
        log.info("所有审批状态的退住记录数量：{}", statusCounts);
        return statusCounts;
    }
    
    /**
     * 查询长者姓名和退住状态
     * 
     * @return 长者姓名和退住状态列表
     */
    @Override
    public List<ElderCheckoutDynamicVO> queryElderNameAndStatus() {
        log.info("开始查询长者姓名和退住状态");
        List<ElderCheckoutDynamicVO> result = checkoutMapper.queryElderNameAndStatus();
        log.info("查询到{}条长者姓名和退住状态记录", result.size());
        return result;
    }
    
    /**
     * 查询退住申请详情
     * 
     * @param realname 长者姓名（可选）
     * @param startTime 开始时间（可选）
     * @param endTime 结束时间（可选）
     * @param sex 性别（可选）
     * @param roomNumber 房间号（可选）
     * @return 退住申请详情列表
     */
    @Override
    public PageResult<ElderCheckoutApplymentVO> queryCheckoutApplyments(Long current,Long size,
            String realname,
            LocalDateTime startTime,
            LocalDateTime endTime,
            Integer sex,
            String roomNumber) {
        log.info("开始查询退住申请详情，条件：realname={}, startTime={}, endTime={}, sex={}, roomNumber={}",
                realname, startTime, endTime, sex, roomNumber);
        Page<ElderCheckoutApplymentVO> page = new Page<>(1, 10);
        Page<ElderCheckoutApplymentVO> result = checkoutMapper.queryCheckoutApplyments(page,
                realname, startTime, endTime, sex, roomNumber);
        return PageResult.of(result);
    }
}
